package com.fau.fm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.fm.entity.MedicalOrdersLog;
import com.fau.fm.service.IOrdersLogManagerService;
import com.fau.fm.util.Page;
import com.fau.fm.util.ParamUtil;

@Service
@Transactional(readOnly = true)
public class OrdersLogManagerService extends BaseService implements
		IOrdersLogManagerService {

	@Override
	public Page findAllOrdersLog(Map<String, Object> map, MedicalOrdersLog log) {
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalOrdersLog a where 1=1 and a.eid=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalOrdersLog a where 1=1 and a.eid=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(log.getEid());
		
		// 订单号
		if(StringUtils.isNotBlank(log.getOrderNo())){
			countSql.append("and a.orderNo = ? ");
			statusSql.append("and a.orderNo = ? ");
			objectList.add(log.getOrderNo());
		}
		// 登录名
		if(StringUtils.isNotBlank(log.getUserName())){
			countSql.append("and a.userName like ? ");
			statusSql.append("and a.userName like ? ");
			objectList.add("%"+log.getUserName()+"%");
		}
		// 用户姓名
		if(StringUtils.isNotBlank(log.getName())){
			countSql.append("and a.name like ? ");
			statusSql.append("and a.name like ? ");
			objectList.add("%"+log.getName()+"%");
		}
		
		statusSql.append("order by a.createTime desc ");
		
		return this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delOrdersLog(MedicalOrdersLog log) {
		String[] ids = log.getId().split(",");
		if(1 == ids.length) {
			this.iBaseDao.deleteObject(log);
		} else {
			for(String id : ids) {
				this.iBaseDao.updateHql("delete from MedicalOrdersLog m where m.id = ? ", id);
			}
		}
	}

}
